﻿using Microsoft.SharePoint;
using System;
using Microsoft.SharePoint.Utilities;
using WeMakeSP.Platform.SharePoint.Configuration;
using System.Collections;
using System.Collections.Generic;

namespace WeMakeSP.ImageResizer.Events
{
    /// <summary>
    /// This class is an helper to add or remove
    /// the image resizer handler from lists.
    /// </summary>
    public static class ImageResizerHandlers
    {
        #region Constants

        /// <summary>
        /// The item receiver class name.
        /// </summary>
        private const string ItemReceiverClassName = "WeMakeSP.ImageResizer.Events.ImageResizerItemEventReceiver";

        #endregion

        #region Public Static Methods

        /// <summary>
        /// Adds the handlers to list.
        /// </summary>
        /// <param name="list">The list.</param>
        public static void AddHandlersToList(SPList list)
        {
            // Guard.

            if (list == null)
            {
                throw new ArgumentNullException("list");
            }
            
            list.EventReceivers.Add(
               SPEventReceiverType.ItemAdded,
               typeof(ImageResizerItemEventReceiver).Assembly.FullName,
               typeof(ImageResizerItemEventReceiver).FullName);

            list.EventReceivers.Add(
               SPEventReceiverType.ItemUpdated,
               typeof(ImageResizerItemEventReceiver).Assembly.FullName,
               typeof(ImageResizerItemEventReceiver).FullName);

            list.Update();
        }

        /// <summary>
        /// Removes the handlers from list.
        /// </summary>
        /// <param name="list">The list.</param>
        public static void RemoveHandlersFromList(SPList list) 
        {
            // Guard.

            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            int i = list.EventReceivers.Count - 1;
            bool deleted = false;

            while (i >= 0)
            {
                SPEventReceiverDefinition eventReceiver =
                    list.EventReceivers[i];

                if (eventReceiver.Class.Equals(ItemReceiverClassName))
                {
                    eventReceiver.Delete();
                    deleted = true;
                }

                i--;
            }

            if (deleted == true)
            {
                list.Update();
            }
        }

        /// <summary>
        /// Removes the handlers from web.
        /// </summary>
        /// <param name="web">The web.</param>
        public static void RemoveHandlersFromWeb(SPWeb web)
        {
            foreach (SPList list in web.Lists) 
            {
                if (list.BaseType == SPBaseType.DocumentLibrary)
                {
                    RemoveHandlersFromList(list);
                }
            }
        }

        #endregion
    }
}